package com.guowei.jiecloudbackstage.staff.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guowei.jiecloudbackstage.common.api.vo.Result;
import com.guowei.jiecloudbackstage.staff.entity.Staff;
import com.guowei.jiecloudbackstage.staff.mapper.StaffMapper;
import com.guowei.jiecloudbackstage.staff.service.IStaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ym
 * @since 2021-04-25
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IStaffService {

    @Autowired
    private StaffMapper staffMapper;

    public Result getAllAdmin() {
        List<Staff> list = new ArrayList<>();
        list = staffMapper.getAllAdmin();
        return Result.ok(200,list);
    }

    @Override
    public Result Login(Staff loginDTO) {
        if (StringUtils.isEmpty(loginDTO.getStaffLoginName())){
            return Result.error(507,"用户名不能为空");
        }
        if (StringUtils.isEmpty(loginDTO.getStaffLoginPwd())){
            return Result.error(508,"密码不能为空");
        }
        //通过登录名查询用户
        QueryWrapper<Staff> wrapper = new QueryWrapper();
        wrapper.eq("staffLoginName", loginDTO.getStaffLoginName());
        Staff uer=staffMapper.selectOne(wrapper);
        System.out.println(uer);
        //比较密码
        if (uer!=null&&uer.getStaffLoginPwd().equals(loginDTO.getStaffLoginPwd())){
            System.out.println("密码对了");
            return Result.ok(200,uer);
        }
        return Result.error(509,"登录失败");
    }

    @Override
    public Result deleteAdminById(Staff staff) {
        int result = staffMapper.deleteAdminById(staff);
        if (result > 0){
            System.out.println("删除管理员用户成功！");
            return Result.ok(200,result);
        }else{
            System.out.println("删除管理员用户失败！");
            return Result.error(512,"删除失败");
        }
    }

    @Override
    public Result addAdmin(Staff staff) {
        int result = staffMapper.addAdmin(staff);
        if (result > 0){
            System.out.println("新增管理员用户成功！");
            return Result.ok(200,result);
        }else{
            System.out.println("新增管理员用户失败！");
            return Result.error(510,"新增失败");
        }
    }

    @Override
    public Result updateAdminById(Staff staff) {
        int result = staffMapper.updateAdminById(staff);
        if (result > 0){
            System.out.println("修改管理员用户成功！");
            return Result.ok(200,result);
        }else{
            System.out.println("修改管理员用户失败！");
            return Result.error(511,"修改失败");
        }
    }

}
